from django.db import models


# Create your models here.
# 创建基类模型 类
class BaseModel(models.Model):
    # auto_now_add=True--- 创建时自动添加时间, 只添加一次
    # auto_now=True--- 每次保存时自动添加时间
    # verbose_name = "创建时间" --- 描述
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        abstract = True  # 抽象类
        ordering = ["-create_time"]  # 默认排序


class Author(models.Model):
    name = models.CharField(max_length=32, verbose_name="作者名")


class Book(BaseModel):
    # db_comment="书名" ---数据表的描述
    name = models.CharField(max_length=32, verbose_name="书名", db_comment="书名")
    price = models.DecimalField(max_digits=5, decimal_places=2, verbose_name="价格")
    # related_name="books" --- 反向关联名称
    author = models.ForeignKey("Author", on_delete=models.CASCADE, related_name="books", verbose_name="作者")

    class Meta(BaseModel.Meta):  # 元类, 显示继承父类元类
        verbose_name = "图书"
        verbose_name_plural = verbose_name
